<?php

/**
 * $Id: user.php 2942 2009-11-30 10:48:18Z huong.pham $
 * Application user component
 * 
 * @author Hung Nguyen
 */
require_once('appcomponent.php');

class User extends AppComponent {

    protected $isGuest = true;
    var $ci;
	var $last_error;
	var $user;
        
    public function __construct() {
        parent::__construct();
        $this->ci =& get_instance();
        $this->ci->load->library('encrypt');              
    }

    public function encode_password($password)
    {        
        return $this->ci->encrypt->encode($password);                
    }
    
    public function decode_password($password)
    {        
        return $this->ci->encrypt->decode($password);                
    }
    public function checkLogin()
    {
        $res = false;
        if($this->ci->session->userdata('user_id') == false)
        {
            $res = false;
        }else
        {
            $res = true;
        }
        return $res;
    }    
    
    public function resetSession()
    {
        
        $newdata = array(
                        'session_id' => $this->ci->session->userdata('session_id'),
                        'ip_address' => $this->ci->session->userdata('ip_address'),
                        'user_agent' => $this->ci->session->userdata('user_agent'),
                        'last_activity' => $this->ci->session->userdata('last_activity'),
                        'user_data' => $this->ci->session->userdata('user_data'),
                        'id' => $this->ci->session->userdata('id'),
                        'email_address'  => $this->ci->session->userdata('email_address'),
                        'first_name' => $this->ci->session->userdata('first_name'),
                        'last_name'     => $this->ci->session->userdata('last_name'),
                        'send_an_update'     => $this->ci->session->userdata('send_an_update'),
                        'msg_zipcode'     => $this->ci->session->userdata('msg_zipcode')
                    );     
        
        foreach (array_keys($this->ci->session->userdata) as $key)
        {
            $this->ci->session->unset_userdata($key);
        }
        $this->ci->session->set_userdata($newdata);
    }    
    
        // For testing ------------------
    public function captcha()
    {        
        // load codeigniter captcha helper
        $this->ci->load->helper('captcha');
        $this->ci->load->helper('string');
        $ramdom_string =random_string('nozero', 5);
        $vals = array(
        'word' => $ramdom_string,
        'img_path'	 => './htdocs/captcha/',
        'img_url'	 => BASEURL.'/htdocs/captcha/',
        'img_width'	 => '150',
        'img_height' => '43',
        'border' => 0, 
        'expiration' => 60
        );
        return create_captcha($vals);
    }
      
    public function getElementFromFullname($name)
    {
        $fullname1 = ltrim($name);
        $fullname = rtrim($fullname1);
        $pos_first = strpos($fullname, " ");
        $data['firstname'] = "";
        $data['lastname'] = "";
        if($pos_first != FALSE)
        {
            $len = $pos_first;
            $data['firstname'] = substr($fullname,0,$len);
            $pos_last = strpos($fullname, " ",$pos_first);
            if($pos_last != FALSE)
            {
                $len = strlen($fullname)-$pos_last;
                $data['lastname'] = substr($fullname,$pos_first+1,$len);
            }
        }else
        {
            $data['firstname'] = $fullname;
        }
        return $data;
    }
}

?>